package com.cloud.point.jvm;

/**
 * try-with-resources语法糖
 *
 * @author 24373
 * @version 1.0
 * @date 2024-06-10 17:03
 */
public class TestTWS {

    /**
     * 异常信息会添加为 压制异常（try-with-resources生成的finally中如果抛出了异常）；防止异常信息的丢失
     */
    public static void main(String[] args) {
        try (MyResource resource = new MyResource()) {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/**
 * 资源对象实现 AutoCloseable 接口，就可以使用 try-with-resources 语法糖
 * - 编译器会帮助生成关闭资源代码
 */
class MyResource implements AutoCloseable {
    @Override
    public void close() throws Exception {
        throw new Exception("close");
    }

}

